package com.ruoyi.tb.lmt.controller;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tb.lmt.domain.bo.TbLmtrepairWorkdetailBo;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbParkingVo;
import com.ruoyi.tb.lmt.domain.bo.TbParkingBo;
import com.ruoyi.tb.lmt.service.ITbParkingService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 存车场信息
 *
 * @author ruoyi
 * @date 2024-01-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/parking")
public class TbParkingController extends BaseController {

    private final ITbParkingService iTbParkingService;

    /**
     * 查询存车场信息列表
     */
    //@SaCheckPermission("xha-lmt:parking:list")
    @GetMapping("/list")
    public TableDataInfo<TbParkingVo> list(TbParkingBo bo, PageQuery pageQuery) {
        return iTbParkingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出存车场信息列表
     */
    //@SaCheckPermission("xha-lmt:parking:export")
    @Log(title = "存车场信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbParkingBo bo, HttpServletResponse response) {
        List<TbParkingVo> list = iTbParkingService.queryList(bo);
        ExcelUtil.exportExcel(list, "存车场信息", TbParkingVo.class, response);
    }

    /**
     * 获取存车场信息详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("xha-lmt:parking:query")
    @GetMapping("/{id}")
    public R<TbParkingVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iTbParkingService.queryById(id));
    }

    /**
     * 新增存车场信息
     */
    //@SaCheckPermission("xha-lmt:parking:add")
    @Log(title = "存车场信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbParkingBo bo) {
        return toAjax(iTbParkingService.insertByBo(bo));
    }

    /**
     * 修改存车场信息
     */
    //@SaCheckPermission("xha-lmt:parking:edit")
    @Log(title = "存车场信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbParkingBo bo) {
        return toAjax(iTbParkingService.updateByBo(bo));
    }

    /**
     * 批量修改存车场信息
     */
    @SaCheckPermission("xha-lmt:parking:updateByBos")
    @Log(title = "存车场信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateByBos")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody List<TbParkingBo> bos) {
        for (TbParkingBo bo : bos) {
            iTbParkingService.updateByBo(bo);
        }
        return R.ok();
    }

    /**
     * 删除存车场信息
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("xha-lmt:parking:remove")
    @Log(title = "存车场信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTbParkingService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 修改存车场信息
     */
    //@SaCheckPermission("xha-lmt:parking:updateParking")
    @Log(title = "修改存车场信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateParking")
    public R<Void> updateParking(@Validated(EditGroup.class) @RequestBody TbParkingBo bo) {
        TbParkingVo tbParkingVo = iTbParkingService.queryById(bo.getId());
        bo.setTrackId(null);
        bo.setTrackName(null);
        bo.setDrivewayName(null);
        if (tbParkingVo != null) {
            if (!bo.getLmtNumber().equals(tbParkingVo.getLmtNumber()) && StringUtils.isNotBlank(bo.getLmtNumber())) {
                TbParkingBo bo1 = new TbParkingBo();
                bo1.setLmtNumber(bo.getLmtNumber());
                List<TbParkingVo> tbParkingVos = iTbParkingService.queryList(bo1);
                if (tbParkingVos.size() > 0) {
                    TbParkingVo tbParkingVo1 = tbParkingVos.get(0);
                    TbParkingBo bo2 = new TbParkingBo();
                    bo2.setId(tbParkingVo1.getId());
                    bo2.setLmtNumber("");
                    bo2.setLmtType("");
                    bo2.setParkingNeed("");
                    TbParkingBo bo3 = iTbParkingService.updateParking(bo2);
                    iTbParkingService.updateByBo(bo3);
                }
            }
        }
        TbParkingBo bos = iTbParkingService.updateParking(bo);
        return toAjax(iTbParkingService.updateByBo(bos));
    }

    /**
     * 根据定位修改存车场信息
     */
    @SaIgnore
    //@SaCheckPermission("xha-lmt:parking:updateByIp")
    @Log(title = "根据定位修改存车场信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateByIp")
    public R<Void> updateParking(String fromIp, String rfidBzm) {

        return toAjax(iTbParkingService.updateByIp(fromIp, rfidBzm));
    }


    @SaIgnore
    @GetMapping("/update")
    public Boolean handleUpdateRequest(
        @RequestParam String date,
        @RequestParam String fromIp,
        @RequestParam String rfidBzm) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date utilDate = sdf.parse(date);  // 解析为 java.util.Date 对象
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        if (StringUtils.isNotBlank(rfidBzm)) {
            rfidBzm = rfidBzm.substring(0, rfidBzm.length() - 4);
        }
        return iTbParkingService.handleUpdateRequest(sqlDate, fromIp, rfidBzm);
    }

    private static List<String> staticFormIps = new ArrayList<>();

    @SaIgnore
    @GetMapping("/update1")
    public void handleUpdateRequest1(
        @RequestParam String date,
        @RequestParam String fromIp,
        @RequestParam String rfidBzm) throws ParseException {
        if (rfidBzm.length() < 28) {
            return;
        }
        if (staticFormIps.contains(fromIp)) {
            return;
        }else {
            staticFormIps.add(fromIp);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date utilDate = sdf.parse(date);  // 解析为 java.util.Date 对象
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        if (StringUtils.isNotBlank(rfidBzm)) {
            rfidBzm = rfidBzm.substring(16, 20);
        }
        String s = iTbParkingService.handleUpdateRequest1(sqlDate, fromIp, rfidBzm);
        if (StringUtils.isNotBlank(s)) {
            System.out.println(s);
        } else {
            System.out.println("成功");
        }
        staticFormIps.remove(fromIp);
    }
}
